package com.itg.vms.servlet;

import com.itg.vms.entity.Movies;
import com.itg.vms.service.CollectMoviesService;
import com.itg.vms.service.impl.CollectMoviesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/apiv2/collect")
public class CollectMoviesServlet extends BaseServlet{
    CollectMoviesService collectMoviesService = new CollectMoviesServiceImpl();

    /**
     * 添加收藏影片
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void addCollectMovies(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        //获取当前登录用户的userid和收藏影片的movieid
         Long movieId = Long.parseLong(req.getParameter("id"));
        Long userId = getUserId(req, resp);
        String username = getUsername(req, resp);

        //调用业务层收藏影片方法
        boolean b = collectMoviesService.addCollectMovies(userId, movieId,username);
        if(b){
            System.out.println("收藏影片成功");
            resp.getWriter().write(success("收藏成功"));
        }else{
            System.out.println("收藏失败");
            resp.getWriter().write("收藏失败");
        }
    }

    /**
     * 查询用户收藏的好片子
      * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void selectAllCollectMovies(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
        Long userId = getUserId(req, resp);
        List<Movies> list = collectMoviesService.selectAllCollectMovies(userId);
        //System.out.println(list);
        resp.getWriter().write(success(list));
    }

    /**
     * 取消收藏影片
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteCollectMovies(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

        Long movieId = Long.parseLong(req.getParameter("id"));
        System.out.println(movieId);
        Long userId = getUserId(req, resp);
        boolean b = collectMoviesService.deleteCollectMovies(userId,movieId);
        if(b){
            System.out.println("取消收藏成功");
            resp.getWriter().write(success("取消收藏成功"));
        }else{
            System.out.println("取消收藏失败");
            resp.getWriter().write(error("出错啦，请联系客服..."));
        }
    }
}
